package com.jack.lease.web.app.service.impl;


import com.jack.lease.model.entity.ImageDao;
import com.jack.lease.web.app.mapper.FileMapper;
import com.jack.lease.web.app.service.FileService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@Slf4j
@Service
public class FileServiceImpl implements FileService {

    @Resource
    private FileMapper fileMapper;

    @Override
    public ResponseEntity<Object> getImage(Long id, String fileName) {
        try {
            ImageDao imageDao = fileMapper.selectImageById(id);
            MediaType mediaType = MediaType.parseMediaType(Files.probeContentType(Path.of(fileName)));
            return ResponseEntity.ok()
                    .contentType(mediaType)
                    .body(imageDao.getBlobData());

        } catch (IOException e) {
            log.error("加载图片出错", e);
            throw new RuntimeException(e);
        }
    }
}
